<?php

if (!defined('_CAN_LOAD_FILES_'))
	exit;

class transferuj extends PaymentModule {
	
	private $_html = '';
	private $_postErrors = array();
	private $id_order;
	
	function __construct()
	{
		$this->name = 'transferuj';
		$this->tab = 'payments_gateways';
		$this->version = '1.0';
		$this->author = 'Transferuj.pl';
		$this->currencies = true;
		$this->currencies_mode = 'radio';
		parent::__construct();
		//$this->page = basename(__FILE__, '.php');
		$this->displayName = $this->l('Transferuj.pl');
		$this->description = $this->l('Obsługa płatności Transferuj.pl');

		if (!sizeof(Currency::checkPaymentCurrencies($this->id)))
			$this->warning = $this->l('Nie ustawiono waluty dla tego modułu');		
	}
	
	function getContent(){
		if (Tools::isSubmit('submit'))
		{
			Configuration::updateValue('TRANSFERUJ_SID', Tools::getValue('sprzedawcaID'));
			Configuration::updateValue('TRANSFERUJ_TIP', Tools::getValue('ip_serw'));
			Configuration::updateValue('TRANSFERUJ_COD', Tools::getValue('kod_potw'));
		}
		
		$this->_displayForm();
		
		return $this->_html;
	}
	
	function _displayForm(){
		$this->_html .='<h2>Transferuj.pl</h2>'.$this->l('Poniżej znajdują się parametry integracji systemu płatności Transferuj.pl.');
		$this->_html .= '
				<form action="'.$_SERVER['REQUEST_URI'].'" method="post">
				<fieldset>
				<legend><img src="../img/admin/contact.gif" />'.$this->l('Główne ustawienia').'</legend>
				<label>'.$this->l('ID Sprzedawcy').'<br /><small style="color:red">'.$this->l('Obowiązkowe').'</small></label>
				<div class="margin-form">
				<input type="text" name="sprzedawcaID" value="'.Configuration::get('TRANSFERUJ_SID').'" />
				</div>
				
				<label>'.$this->l('Kod potwierdzający').'<br /><small>'.$this->l('Taki sam jak w Panelu sprzedawcy').'</small></label>
				<div class="margin-form">
				<input type="text" name="kod_potw" value="'.Configuration::get('TRANSFERUJ_COD').'" maxlength="16" />
				</div>
					
				<label>'.$this->l('IP serwera').'<br /><small style="color:red">'.$this->l('Obowiązkowe').'</small></label>
				<div class="margin-form">
				<input type="text" name="ip_serw" value="'.Configuration::get('TRANSFERUJ_TIP').'" />
				</div>	
				<br /><br />
				<input type="submit" name="submit" value="'.$this->l('Zapisz Ustawienia').'" class="button" />
				</fieldset>
				</form>';
		
	}
	
	function install()
	{
		if (!parent::install() OR !$this->registerHook('payment') OR !$this->registerHook('paymentReturn'))
			return false;
		Configuration::updateValue('TRANSFERUJ_TIP', '195.149.229.109');
		return true;
	}
	
	function uninstall()
	{
		return (parent::uninstall());
	}	
	
	function hookPayment($params)
	{
		if (!$this->active)
			return;
		
		global $smarty, $cookie;

		$smarty->assign('this_path', $this->_path);
		//$smarty->assign('this_path_ssl', (Configuration::get('PS_SSL_ENABLED') ? 'https://' : 'http://').htmlspecialchars($_SERVER['HTTP_HOST'], ENT_COMPAT, 'UTF-8').__PS_BASE_URI__.'modules/'.$this->name.'/');
		$smarty->assign('buttonText', $this->l('Płać z Transferuj.pl')); 
		
		return $this->display(__FILE__, 'transferuj.tpl');
	}
	
	function hookPaymentReturn($params){
		if (!$this->active)
			return ;

		return $this->display(__FILE__, 'confirmation.tpl');
	}
	
	function preparePayment()
	{
		
		global $smarty, $cart, $cookie;
		$kraj_iso = LanguageCore::getIsoById($cookie->id_lang);
		$currency = $this->getCurrency((int)$cart->id_currency);

		if ($cart->id_currency != $currency->id)
		{
			$cart->id_currency = (int)$currency->id;
			$cookie->id_currency = (int)$cart->id_currency;
			$cart->update();
			Tools::redirect('modules/'.$this->name.'/payment.php');
		}		
		
		$opistr="Zamówienie nr: ".$cart->id;
		$address = new Address(intval($cart->id_address_invoice));
		$customer = new Customer(intval($cart->id_customer));
		$kwota=number_format($cart->getOrderTotal(true), 2, '.', ''); // floatval nie jest potrzebne

		$sid = Configuration::get('TRANSFERUJ_SID'); //id sprzedawcy
		$kod = Configuration::get('TRANSFERUJ_COD'); // kod potwierdzający
		$crc = base64_encode($cart->id); // utworzenie parametru pomocniczego na podstawie id koszyka
		$smarty->assign(array(
			'kwota'=>$kwota,
			'mail'=>$customer->email,
			'nazwisko'=>$customer->lastname,
			'imie'=>$customer->firstname,
			'telefon'=>$address->phone,
			'adres'=>$address->address1." ".$address->address2,
			'miasto'=>$address->city,
			'kod'=>$address->postcode,
			'BackUrl' => 'http://'.htmlspecialchars($_SERVER['HTTP_HOST'], ENT_COMPAT, 'UTF-8').__PS_BASE_URI__,
			'goBackUrl' => 'http://'.htmlspecialchars($_SERVER['HTTP_HOST'], ENT_COMPAT, 'UTF-8').__PS_BASE_URI__.'order-confirmation.php?key='.$customer->secure_key.'&id_cart='.intval($cart->id).'&id_module='.intval($this->id),
			'notify' => 'http://'.htmlspecialchars($_SERVER['HTTP_HOST'], ENT_COMPAT, 'UTF-8').__PS_BASE_URI__.'modules/transferuj/validation.php',
			'jezyk' => $kraj_iso,
			'kraj'=>$address->country,
			'sid'=> $sid,
			'otr'=> $opistr,
			'crc'=> $crc,
			'seckey'=> $customer->secure_key,
			'md5sum'=> md5($sid.$kwota.$crc.$kod),
			//'tip'=>Configuration::get('TRANSFERUJ_TIP'), //adres serwera
			'this_path' => $this->_path,
			'this_path_ssl' => (Configuration::get('PS_SSL_ENABLED') ? 'https://' : 'http://').htmlspecialchars($_SERVER['HTTP_HOST'], ENT_COMPAT, 'UTF-8').__PS_BASE_URI__.'modules/'.$this->name.'/'
			));
		
	}
}
?>